<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of UrlsService
 *
 * @author Administrator
 */
class UrlsService {

    function insert($url) {
        D('urls')->add($url);
    }

    function insertAll($urls) {
        $urlsTable = D('urls');
        $urlsTable->startTrans();

        $lastId = $urlsTable->addAll($urls);
        $urlsTable->commit();

        return $lastId;
    }

    /**
     * 当前类型的Url是否已经抓取
     * @param type $url
     * @param type $type 
     */
    function isHas($url, $type) {
        $urlsTable = D('urls');
        $where['url'] = $url;
        $where['type'] = $type;

        $count = $urlsTable->where($where)->find();

        return is_null($count);
    }

    /**
     * 发布完毕
     */
    function isReleaseOver($url, $type) {
        
    }

    /**
     * 获取该类型的未发布的数据
     * @param type $type 
     */
    function findReleaseByType($type) {
        $urlsTable = D('urls');
        $where['type'] = $type;
        $where['load_over'] = 0;
        return $urlsTable->where($where)->order('datetime asc')->limit(1)->find();
    }

    /**
     * 将数据更新为已发布
     * @param type $urlId 
     */
    function updateUrls($urlId) {
        $urlsModel = new Model();
        $urlsModel->startTrans();

        $lastId = $urlsModel->execute('update urls set load_over = 1 where id = ' . $urlId . '');
        $urlsModel->commit();

        return $lastId;
    }

    /**
     * 查询Urls中数据
     * @param type $url
     * @param type $type
     * @return type 
     */
    function findUrlsByUrl($url, $type='app') {
        $urlsTable = D('urls');
        $where['url'] = array('eq', $url);
        $where['type'] = array('eq', $type);

        return $urlsTable->where($where)->find();
    }

}

?>
